const fs = require('fs');
const express = require('express');
const app = express();
const https = require('https');
let options = {
    key: fs.readFileSync('./ca.key'),
    ca: fs.readFileSync('./ca.csr'),
    cert: fs.readFileSync('./ca.crt')
};
const server = https.createServer(options, app);
const { Server } = require('socket.io');
const io = new Server(server);
let mainUser = null;
let mainIce = null;

app.get('/', (req, res) => {
    res.send('<h1>Hello world</h1>');
});
 
app.get('/test', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
    socket.on('message', (msg) => {
        console.log(mainUser);
        if(!mainUser) {
            mainUser = socket;
            mainIce = msg;
            return
        } else {
            mainUser.emit('message', mainIce)
        }
    })
});

server.listen(3000, () => {
    console.log('listening on *: 3000')
})